//
//  YDOAApprovalVC.swift
//  YDOAProduct
//
//  Created by 占益民 on 2017/11/7.
//  Copyright © 2017年 Allen. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class YDOAApprovalVC: UICollectionViewController {
    
    
    
    lazy var dataSource : NSMutableArray = {
        var arr = NSMutableArray();
        return arr;
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView?.backgroundColor = UIColor.white
        self.navigationController?.navigationBar.isHidden = false
        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false
        
//
//         self.dataSource.addObjects(from: ["固定资产","车辆管理","月度办公","办公领用","急性办公","报销管理"])
        
        self.loadData()
        // Register cell classes
        self.collectionView?.register(UINib.init(nibName: "YDOAApprovalItem", bundle:nil), forCellWithReuseIdentifier: reuseIdentifier);

        // Do any additional setup after loading the view.
    }

    //    MARK :加载数据
    func loadData(){
        let userinfo = UserDefaults.standard.object(forKey: USERINFO) as! [String : AnyObject]
        let uid = userinfo["id"]!
        let urlStr = String(format:GET_STAY_APPROVAL,uid as! CVarArg)
        YMLog.log(item: urlStr)
        YMNetworkManager().getRequest(urlString: urlStr, params:nil, success: { (respenser) in
            YMLog.log(item: respenser)
            if !respenser.isEmpty && respenser["status"] as! String == "1"{
                for dict in respenser["data"] as! NSArray {
                    let model = dict as! [String:AnyObject]
                    self.dataSource.add(model)
                    self.collectionView?.reloadData()
                }
            }else{
                
            }
            
        }) { (error) in
            
        }
        
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    // MARK: UICollectionViewDataSource

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return self.dataSource.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as? YDOAApprovalItem else{return UICollectionViewCell()}
    
        // Configure the cell
        let model = self.dataSource[indexPath.row] as! [String:AnyObject]
        print(model);
        cell.badgLb.text = model["count"] as? String
        cell.badgLb.isHidden =  (model["count"] as! String != "0" ) ? false : true
        
//        cell.titlelab.text = model[""] as? String;
        if model["type"]?.int8Value == 1 {
            cell.titlelab.text = "办公用品"
        }else if model["type"]?.int8Value == 2{
            cell.titlelab.text = "月度需求"
        }else if model["type"]?.int8Value == 3{
            cell.titlelab.text = "急性物品"
        }else if model["type"]?.int8Value == 0{
            cell.titlelab.text = "费用报销"
        }else if model["type"]?.int8Value == 5{
            cell.titlelab.text = "派车申请"
        }else if model["type"]?.int8Value == 4{
            cell.titlelab.text = "固资申购"
        }
        
    
        return cell
    }
    
    
    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let  vc =  YDOAFixedApprovalVC()
        let model = self.dataSource[indexPath.row] as! [String:AnyObject]
        let type = model["type"]?.int8Value
        vc.type = type
        self.navigationController?.pushViewController(vc, animated: true)
//        switch indexPath.item {
//
//        case 0:
//            //固定资产审批
//
//            self.navigationController?.pushViewController(vc, animated: true)
//            break
//        case 1:
//            //车辆管理审批
//            self.navigationController?.pushViewController(vc, animated: true)
//            break
//        default:
//            break
//        }
    }

    
}
